package com.infolab.aims.domain.asset;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.infolab.aims.common.SpecialMuLineToStringStyle;
import com.infolab.aims.common.SpecialToStringStyle;
import com.infolab.aims.common.bean.AbstractModelBean;
import com.infolab.aims.domain.Department;
import com.infolab.aims.domain.Location;
import com.infolab.aims.domain.Vendor;

@Entity
@Table(name="AMTB_ASSET_TRANSACTION")
@NamedQueries({
@NamedQuery(name = "AssetTransaction.findByAssetModel", query = "from AssetTransaction a where a.assetTransactionId in (select assetTransactionItemPK.assetTransaction.assetTransactionId  from AssetTransactionItem where assetModel.assetModelId=:assetModelId)")
})
public class AssetTransaction extends AbstractModelBean implements Serializable {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer assetTransactionId;
	
	private String assetTransactionCode;
	
	private String assetType;
	
	/* stock adjustment, stock transfer*/
	private String assetTransactionType;
	
	/* in/out */
	private String assetTransactionSubType;
		
	private String assetTransactionStatus;
	
	private Date approvedDate;
	
	private String approvedBy;
	
	private Date rejectedDate;
	
	private String rejectedBy;
	
	private String companyCode;
	

	private String toCompanyCode;
	
	@Transient
	private Vendor vendor;
	
	public String getToCompanyCode() {
		return toCompanyCode;
	}

	public void setToCompanyCode(String toCompanyCode) {
		this.toCompanyCode = toCompanyCode;
	}

	private String remarks;
	
	@ManyToOne
	@JoinColumn(name="accessDepartmentId")
	private Department accessDepartment;
	
	@ManyToOne
	@JoinColumn(name="fromLocationId",referencedColumnName="locationId")
	private Location fromLocation;
	
	@ManyToOne
	@JoinColumn(name="toLocationId",referencedColumnName="locationId")
	private Location toLocation;
	
	@OneToMany(mappedBy="assetTransactionItemPK.assetTransaction")
	private List<AssetTransactionItem> assetTransactionItems;
	
	@Transient
	private List<AssetTransactionItem> assetTransactionItemsForm = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(AssetTransactionItem.class));

	public Integer getAssetTransactionId() {
		return assetTransactionId;
	}

	public void setAssetTransactionId(Integer assetTransactionId) {
		this.assetTransactionId = assetTransactionId;
	}

	public String getAssetTransactionCode() {
		return assetTransactionCode;
	}

	public void setAssetTransactionCode(String assetTransactionCode) {
		this.assetTransactionCode = assetTransactionCode;
	}

	public String getAssetType() {
		return assetType;
	}

	public void setAssetType(String assetType) {
		this.assetType = assetType;
	}

	public String getAssetTransactionType() {
		return assetTransactionType;
	}

	public void setAssetTransactionType(String assetTransactionType) {
		this.assetTransactionType = assetTransactionType;
	}

	public String getAssetTransactionStatus() {
		return assetTransactionStatus;
	}

	public void setAssetTransactionStatus(String assetTransactionStatus) {
		this.assetTransactionStatus = assetTransactionStatus;
	}

	public Department getAccessDepartment() {
		return accessDepartment;
	}

	public void setAccessDepartment(Department accessDepartment) {
		this.accessDepartment = accessDepartment;
	}

	public Location getFromLocation() {
		return fromLocation;
	}

	public void setFromLocation(Location fromLocation) {
		this.fromLocation = fromLocation;
	}

	public Location getToLocation() {
		return toLocation;
	}

	public void setToLocation(Location toLocation) {
		this.toLocation = toLocation;
	}
	
	public List<AssetTransactionItem> getAssetTransactionItems() {
		return assetTransactionItems;
	}

	public void setAssetTransactionItems(
			List<AssetTransactionItem> assetTransactionItems) {
		this.assetTransactionItems = assetTransactionItems;
	}

	public List<AssetTransactionItem> getAssetTransactionItemsForm() {
		return assetTransactionItemsForm;
	}

	public void setAssetTransactionItemsForm(
			List<AssetTransactionItem> assetTransactionItemsForm) {
		this.assetTransactionItemsForm = assetTransactionItemsForm;
	}
	
	public boolean isIssueOrderComponent()
	{
		return StringUtils.equalsIgnoreCase(TRANSACTIONTYPE_ISSUEORDER, assetTransactionType);
	}
	
	public boolean isEquipmentTransfer()
	{
		return StringUtils.equalsIgnoreCase(TRANSACTIONTYPE_EQUIPMENTTRANSFER, assetTransactionType);
	}
	
	public boolean isFaultyEquipmentTransfer()
	{
		return StringUtils.equalsIgnoreCase(TRANSACTIONTYPE_FAULTYTRANSFER, assetTransactionType);
	}
	
	public boolean isInHouseTransfer()
	{
		return StringUtils.equalsIgnoreCase(TRANSACTIONTYPE_INHOUSETRANSFER, assetTransactionType);
	}

	public Date getApprovedDate() {
		return approvedDate;
	}

	public void setApprovedDate(Date approvedDate) {
		this.approvedDate = approvedDate;
	}

	public Date getRejectedDate() {
		return rejectedDate;
	}

	public void setRejectedDate(Date rejectedDate) {
		this.rejectedDate = rejectedDate;
	}

	public String getApprovedBy() {
		return approvedBy;
	}

	public void setApprovedBy(String approvedBy) {
		this.approvedBy = approvedBy;
	}

	public String getRejectedBy() {
		return rejectedBy;
	}

	public void setRejectedBy(String rejectedBy) {
		this.rejectedBy = rejectedBy;
	}

	public String getCompanyCode() {
		return companyCode;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getAssetTransactionSubType() {
		return assetTransactionSubType;
	}

	public void setAssetTransactionSubType(String assetTransactionSubType) {
		this.assetTransactionSubType = assetTransactionSubType;
	}
	
	public String getAuditContentHead()
	{
		Location fromLoc = fromLocation;
		Location toLoc = toLocation;
		if(fromLocation==null){
			fromLoc = new Location();
			fromLoc.setLocationCode("");
			fromLoc.setLocationName("");
		}
		if(toLocation==null){
			toLoc = new Location();
			toLoc.setLocationCode("");
			toLoc.setLocationName("");
		}
		
		
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
		.append("Issue Order Code", assetTransactionCode)
		.append("Issue Order Type", assetTransactionType)
		.append("Issue Order Date", this.getLastModifiedDate())
		.append("Issue Order Status",assetTransactionStatus)
		.append("From Location", fromLoc.getLocationCode() + " " + fromLoc.getLocationName())
		.append("To Location", toLoc.getLocationCode() + " " + toLoc.getLocationName())
		.append("Company Code", companyCode)
		.toString();
	}
	
	public String getAuditContentDetail()
	{
		ToStringBuilder builder = new ToStringBuilder(this,SpecialMuLineToStringStyle.SPECIAL_MULINE_STYLE);
		if(CollectionUtils.isNotEmpty(assetTransactionItems))
		{
			int i = 0;
			for(AssetTransactionItem assetTransactionItem : assetTransactionItems)
			{
				if(!assetTransactionItem.isDeleted()){
					builder.append("Item " + (++i), assetTransactionItem.getAuditContentHead());
				}
			}
			String str = builder.toString();
			if(str.length()>2000){
				str=str.substring(0,1997)+"..]";
			}
			return StringUtils.substring(str, str.indexOf("[")+1, str.lastIndexOf("]")-1);
		}
		return super.getAuditContentDetail();
	}
	
	public String getAuditCode()
	{
		if(StringUtils.isNotEmpty(assetTransactionCode)){
			return assetTransactionCode;
		}
		return super.getAuditCode();
	}
	
	
	
	public String getAuditContentDetailForInitialBalance()
	{
		ToStringBuilder builder = new ToStringBuilder(this,SpecialMuLineToStringStyle.SPECIAL_MULINE_STYLE);
		if(CollectionUtils.isNotEmpty(assetTransactionItems))
		{
			int i = 0;
			for(AssetTransactionItem assetTransactionItem : assetTransactionItems)
			{
				if(!(assetTransactionItem.isDeleted()) && assetTransactionItem.getAssetTransactionQuantity()!=null){
					builder.append("Item " + (++i), assetTransactionItem.getAuditContentHeadForInitialBalance());
				}
			}
			String str = builder.toString();
			if(str.length()>2000){
				str=str.substring(0,1997)+"..]";
			}
			return StringUtils.substring(str, str.indexOf("[")+1, str.lastIndexOf("]")-1);
		}
		return super.getAuditContentDetail();
	}
	
	
	public String getAuditContentHeadForInitialBalance()
	{
		String assetModelName="";
		if(CollectionUtils.isNotEmpty(assetTransactionItems))
		{
			AssetTransactionItem assetTransactionItem=assetTransactionItems.get(0);
			{
				assetModelName = assetTransactionItem.getAssetModel().getAssetModelName().trim();
			}
		}
		
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
		.append("Initial Bal. saved for", assetModelName)
		.toString();
		
	}	
	
	public Vendor getVendor() {
		return vendor;
	}

	public void setVendor(Vendor vendor) {
		this.vendor = vendor;
	}

	public static final String TRANSACTIONTYPE_INITIALBALANCE="IBL";
	public static final String TRANSACTIONTYPE_ISSUEORDER="IO";
	public static final String TRANSACTIONTYPE_WORKORDER="WO";
	public static final String TRANSACTIONTYPE_EQUIPMENTTRANSFER="ET";
	public static final String TRANSACTIONTYPE_FAULTYTRANSFER="FT";
	public static final String TRANSACTIONTYPE_STOCKADJUSTMENT="SAJ";
	public static final String TRANSACTIONTYPE_DELIVERYORDER="DO";
	public static final String TRANSACTIONTYPE_SALESOFITEMS="SOI";
	public static final String TRANSACTIONTYPE_INHOUSETRANSFER="INH";
	public static final String TRANSACTIONTYPE_PURCHASEORDERREPAIR="POR";
	public static final String TRANSACTIONTYPE_DELIVERYORDERREPAIR="DOR";
	
	public static final String TRANSACTIONSUBTYPE_IN="IN";
	public static final String TRANSACTIONSUBTYPE_OUT="OUT";
	
	public static final String TRANSACTIONSTATUS_PENDING="PENDING";
	public static final String TRANSACTIONSTATUS_APPROVED="APPROVED";
	public static final String TRANSACTIONSTATUS_REJECTED="REJECTED";
}
